
import { Card, CardHeader, CardTitle, CardContent } from '../components/ui/card';
import { Badge } from '../components/ui/badge';
import { Button } from '../components/ui/button';
import Layout from '../components/Layout';
import { useUsersQuery } from '../hooks/useQueries';
import { useUserActions } from '../store/useStore';

export default function UsersPage() {
  const { data: users, isLoading, error } = useUsersQuery();
  const { setCurrentUser } = useUserActions();

  if (isLoading) {
    return (
      <Layout>
        <div className="space-y-4">
          <h1 className="text-3xl font-bold">Users</h1>
          <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
            {Array.from({ length: 6 }).map((_, i) => (
              <Card key={i} className="animate-pulse">
                <CardHeader>
                  <div className="h-4 bg-muted rounded w-3/4"></div>
                  <div className="h-3 bg-muted rounded w-1/2"></div>
                </CardHeader>
                <CardContent>
                  <div className="space-y-2">
                    <div className="h-3 bg-muted rounded"></div>
                    <div className="h-3 bg-muted rounded w-5/6"></div>
                  </div>
                </CardContent>
              </Card>
            ))}
          </div>
        </div>
      </Layout>
    );
  }

  if (error) {
    return (
      <Layout>
        <div className="space-y-4">
          <h1 className="text-3xl font-bold">Users</h1>
          <Card className="border-destructive">
            <CardContent className="pt-6">
              <p className="text-destructive">
                Error loading users: {error.message}
              </p>
            </CardContent>
          </Card>
        </div>
      </Layout>
    );
  }

  return (
    <Layout>
      <div className="space-y-6">
        <div className="flex items-center justify-between">
          <h1 className="text-3xl font-bold">Users</h1>
          <Badge variant="outline">
            {users?.length || 0} users
          </Badge>
        </div>

        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
          {users?.map((user) => (
            <Card key={user.id} className="hover:shadow-lg transition-shadow">
              <CardHeader>
                <div className="flex items-center justify-between">
                  <CardTitle className="text-lg">{user.name}</CardTitle>
                  <Badge variant="secondary">#{user.id}</Badge>
                </div>
                <p className="text-sm text-muted-foreground">@{user.username}</p>
              </CardHeader>
              <CardContent>
                <div className="space-y-3">
                  <div>
                    <p className="text-sm font-medium">Email</p>
                    <p className="text-sm text-muted-foreground">{user.email}</p>
                  </div>
                  <div>
                    <p className="text-sm font-medium">Company</p>
                    <p className="text-sm text-muted-foreground">{user.company.name}</p>
                  </div>
                  <div>
                    <p className="text-sm font-medium">Location</p>
                    <p className="text-sm text-muted-foreground">
                      {user.address.city}, {user.address.zipcode}
                    </p>
                  </div>
                  <div className="flex gap-2 pt-2">
                    <Button asChild size="sm" variant="outline">
                      <a href={`/users/${user.id}`}>View Details</a>
                    </Button>
                    <Button 
                      size="sm" 
                      variant="secondary"
                      onClick={() => setCurrentUser(user)}
                    >
                      Set as Current
                    </Button>
                  </div>
                </div>
              </CardContent>
            </Card>
          ))}
        </div>
      </div>
    </Layout>
  );
} 